const App = getApp()
import { ManagerModel } from '../../../models/manager'
import { AppModel } from '../../../models/app.js'
import { MineModel } from '../../../models/mine.js'
const ModelMine = new MineModel()
const ModelApp = new AppModel()
const managerModel = new ManagerModel()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    isOpen: false,
    userFans: null,
    sales: 0,
    vipUserRebate: null,
    num: 0,
    yellowNum:0,
    showService: false,
    serviceInfo: {},
    showCanvas: false,
    template: {},
    shareData: {
      mode: ''
    },
    statusBarHeight: App.globalData.statusBarHeight,
    earningsAll: 0, // 总发放绩效
    giveSalesAll: 0, // 实时总绩效
    totalSalesAll: 0, // 团队总佣金
    dayAchievements: 0, // 今日绩效
    amount: 0, // 会员卡价格
    showWx: false, // 是否展示专属顾问
    inviteInfo: {}, // 上级信息
    inviteVisible: false // 专属顾问信息
  },

  // tab切换
  tabClick(e) {
    const index = e.currentTarget.dataset.index
    if (index == this.data.active) {
      return
    } else {
      this.setData({
        active: index
      })
      this.initData(this.data.active)
    }
  },

  // 显示专属顾问信息
  showInvite() {
    this.setData({
      inviteVisible: true
    })
  },

  // 关闭显示专属顾问信息
  closeInvite() {
    this.setData({
      inviteVisible: false
    })
  },

  // 联系专属顾问
  invite() {
    const parentId = App.globalData.userInfo.parentId
    if(parentId) {
      const params = {
        id: parentId
      }
      ModelMine.getBandWxInfo(params).then(res => {
        // 开
        if (res.data.switch == 1) {
          if(res.data.inviteCode) {
            this.setData({
              inviteInfo: res.data,
              showWx: true
            })
          } else {
            this.setData({
              showWx: false
            })
          }
        }else{
          this.setData({
            showWx: false
          })
        }
      }).catch()
    }
  },

  // 购买黄金会员
  tapToBuy() {
    wx.navigateTo({
      url: `/pages/manager/buy/index?amount=` + this.data.amount
    })
  },

  //跳转至佣金页面
  tapToCommision: function(event) {
    wx.navigateTo({
      url: `/pages/manager/commision/index/index?active=`+this.data.active
    })
  },

  //跳转至粉丝页面
  tapToFollower: function(event) {
    wx.navigateTo({
      url: `/pages/manager/follower/index`
    })
  },

  //跳转至任务页面
  tapToTask: function(event) {
    wx.navigateTo({
      url: `/pages/manager/task/index`
    })
  },

  //切换显示隐藏邀请人
  inviterSwitch: function() {
    this.setData({
      isOpen: !this.data.isOpen
    })
  },

  //查看邀粉指南
  checkGuide:function(){
    wx.navigateTo({
      url: `/pages/manager/guide/index`
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.initData(this.data.active)
    this.invite()
  },

  initData: function(active) {
    const that = this
    //获取当前用户类型，佣金，粉丝，邀请人信息
    if (active == 0) {
      managerModel.getManager().then(res => {
        //判断是否用佣金
        const vipUserRebate = !res.data.vipUserRebate
          ? that.data.vipUserRebate
          : res.data.vipUserRebate
          that.setData({
            userFans: res.data.userFans,
            sales: res.data.sales ? res.data.sales : that.data.sales,
            vipUserRebate: vipUserRebate,
            num: res.data.num,
            yellowNum:res.data.yellowNum,
            amount: res.data.amount
          })
      })
    } else {
      managerModel.queryAllCentert().then(res => {
        that.setData({
          earningsAll: res.data.earningsAll,
          giveSalesAll: res.data.giveSalesAll,
          totalSalesAll: res.data.totalSalesAll,
          dayAchievements: res.data.dayAchievements
        })
      }).catch()
    }
  },
  // 组件触发显示售后
  showService() {
    const _this = this
    App.queryServiceInfo(function(serviceInfo) {
      _this.setData({
        showService: true,
        serviceInfo: serviceInfo
      })
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.initData(this.data.active)
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},
  // 只有图片分享
  doShare() {
    // 分享
    this.setData({
      shareData: {
        showWay: 'nobar' // 不弹出底部分享
      }
    })
  },
  // doShare(event) {
  //   // 分享
  //   let _this = this
  //   // 初始化图步
  //   const canvas = new Canvas()
  //   const params = {
  //     scene: App.globalData.userInfo.userCode,
  //     page: 'pages/home/index',
  //     width: 400,
  //     auto_color: false,
  //     r: '0',
  //     g: '0',
  //     b: '0',
  //     is_hyaline: false
  //   }
  //   // loading
  //   wx.showLoading({
  //     title: '加载中'
  //   })
  //   console.log(`home==${App.globalData.userInfo.userCode}`)
  //   // 获取小程序二维码
  //   ModelApp.getCodeUrl(params)
  //     .then(res => {
  //       const codeImg = res.codeUrl
  //       const template = canvas.home(
  //         App.globalData.userInfo.userImg,
  //         decodeURIComponent(App.globalData.userInfo.nickName), // 解码昵称
  //         codeImg
  //       )
  //       _this.setData({
  //         template: template
  //       })
  //       setTimeout(() => {
  //         // loading
  //         wx.hideLoading()
  //         _this.setData({
  //           showCanvas: true
  //         })
  //       }, 800)
  //     })
  //     .catch(err => {})
  // },
  // 显示会员等级介绍
  tapToLevRouter() {
    const bannerUrl = 'https://admin.top1buyer.com/wxview/images'
      const query = 's-newer-app.png'
      wx.navigateTo({
        url: `/pages/home/webview/index?bannerUrl=${bannerUrl}&query=${query}`
      })
  }
})
